﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Snake.Net;

namespace Snake
{
    public partial class Frm_Login : Form
    {
        Frm_Game frm_Main;
        public Frm_Login()
        {
            InitializeComponent();
            Size = new Size(800, 400);
            this.BackColor = Backcolor;
            this.MouseDown += gPanelTitleBack_MouseDown;
            label2.MouseDown += gPanelTitleBack_MouseDown;

        }

        bool InputFlag = false;

        protected static string UserId;
        protected static string Pw;

        Pen pen1 = new Pen(Color.White, 1.5f);
        Pen pen2 = new Pen(Color.White, 1.5f);
        Color Backcolor = Color.FromArgb(102, 204, 255);

        protected override void OnPaint(PaintEventArgs e)
        {

            Graphics g = e.Graphics;

            g.SmoothingMode = SmoothingMode.HighQuality;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality; //高像素偏移质量
            g.Clear(BackColor);
            g.DrawLine(pen1, 250, 220, 550, 220);
            g.DrawLine(pen2, 250, 300, 550, 300);
            g.FillEllipse(Brushes.White, 340, 30, 120, 110);

            Pen pen3 = new Pen(BackColor, 3);
            g.DrawEllipse(pen3, 400 - 25, 45, 50, 50);
            g.DrawLine(pen3, 400 - 25, 45 + 50 + 8, 400 + 25, 45 + 50 + 8);
            g.DrawLine(pen3, 400 - 25, 45 + 50 + 8, 400 - 40, 45 + 50 + 30);
            g.DrawLine(pen3, 400 + 40, 45 + 50 + 30, 400 + 25, 45 + 50 + 8);
            //g.DrawLine(Pens.Black, 400 - 40, 45 + 50 + 30, 400 + 40, 45 + 50 + 30);


            Pen pen = new Pen(Color.Black) { Width = 0.2F };
            Font goodFont = new Font("微软雅黑", 20);
            StringFormat strFormat = new StringFormat();
            strFormat.LineAlignment = StringAlignment.Far;
            strFormat.Alignment = StringAlignment.Center;//水平居中
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;//抗锯齿

            Rectangle rec_id = new Rectangle(250, 170, 300, 50);
            Rectangle rec_pw = new Rectangle(250, 250, 300, 50);

            string id = UserId == null || UserId == string.Empty ? "User Name" : UserId;
            string pw = Pw == null || Pw == string.Empty ? "Password" : Pw;




            if (!string.IsNullOrEmpty(Pw))
            {
                pw = "";
                foreach (char chr in Pw)
                {
                    pw += "*";
                }
            }


            g.DrawString(id, goodFont, Brushes.White, rec_id, strFormat);
            g.DrawString(pw, goodFont, Brushes.White, rec_pw, strFormat);

        }

        private void Frm_Log_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.X > 250 && e.X < 550)
            {
                if (e.Y > 170 && e.Y < 220)
                {
                    pen2 = new Pen(Color.White, 1.5f);
                    pen1 = new Pen(Color.White, 2f);
                    InputFlag = false;
                    textBox1.Text = UserId;
                }
                if (e.Y > 250 && e.Y < 300)
                {
                    pen1 = new Pen(Color.White, 1.5f);
                    pen2 = new Pen(Color.White, 2f);
                    InputFlag = true;
                    textBox1.Text = Pw;
                }
            }
            this.Invalidate(true);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            textBox1.Focus();

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (InputFlag)
            {
                Pw = textBox1.Text;
            }
            else
            {
                UserId = textBox1.Text;
            }
            this.textBox1.Select(this.textBox1.TextLength, 0);
            Invalidate(true);
        }


        //定义无边框窗体Form

        [DllImport("user32.dll")]//*********************拖动无窗体的控件
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        private void gPanelTitleBack_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);//*********************调用移动无窗体控件函数
        }


        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    {
                        if (string.IsNullOrWhiteSpace(UserId) || string.IsNullOrWhiteSpace(Pw)) return;
                        var result = HttpRequest.Login(UserId, Pw);
                        if (result == "OK")
                        {
                            //MessageBox.Show("登陆成功");
                            Hide();
                            new Frm_Barrier(UserId).ShowDialog();
                        }
                        else
                        {
                            MessageBox.Show(result);
                        }
                        break;
                    }
            }
        }
    }
}
